package com.ruoyi.web.controller.proreportcontactsheet;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.ProReportContactSheet;
import com.ruoyi.service.IProReportContactSheetService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 维保报告书联系单Controller
 * 
 * @author ruoyi
 * @date 2020-07-04
 */
@Controller
@RequestMapping("/ruoyi/sheet1")
public class ProReportContactSheetsController extends BaseController
{
    private String prefix = "ruoyi/sheet";

    @Autowired
    private IProReportContactSheetService proReportContactSheetService;

    @RequiresPermissions("ruoyi:sheet1:view")
    @GetMapping()
    public String sheet()
    {
        return prefix + "/sheet2";
    }

    /**
     * 查询维保报告书联系单列表
     */
    @RequiresPermissions("ruoyi:sheet1:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProReportContactSheet proReportContactSheet)
    {

        List<ProReportContactSheet> list = proReportContactSheetService.selectProReportContactSheetList(proReportContactSheet);
        list.removeIf(proReportContactSheet1 -> proReportContactSheet1.getmCheckProblem()==null);
        list.removeIf(proReportContactSheet1 -> proReportContactSheet1.getmMforPayment()==null);
        startPage();
        return getDataTable(list);
    }

    /**
     * 导出维保报告书联系单列表
     */
    @RequiresPermissions("ruoyi:sheet1:export")
    @Log(title = "维保报告书联系单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProReportContactSheet proReportContactSheet)
    {
        List<ProReportContactSheet> list = proReportContactSheetService.selectProReportContactSheetList(proReportContactSheet);
        ExcelUtil<ProReportContactSheet> util = new ExcelUtil<ProReportContactSheet>(ProReportContactSheet.class);
        return util.exportExcel(list, "sheet");
    }

    /**
     * 新增维保报告书联系单
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/adds";
    }

    /**
     * 新增保存维保报告书联系单
     */
    @RequiresPermissions("ruoyi:sheet1:add")
    @Log(title = "维保报告书联系单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(
//            @RequestParam("file") MultipartFile file,
//                              @RequestParam("file1") MultipartFile file1,
                              @RequestParam("file2") MultipartFile file2,
                              @RequestParam("file3") MultipartFile file3,
                              ProReportContactSheet proReportContactSheet)throws IOException
    {
        // 上传文件路径
        String filePath = Global.getUploadPath();
        // 上传并返回新文件名称
//        String fileName = FileUploadUtils.upload(filePath, file);
//        String fileName1 = FileUploadUtils.upload(filePath, file1);
        String fileName2 = FileUploadUtils.upload(filePath, file2);
        String fileName3= FileUploadUtils.upload(filePath, file3);
        proReportContactSheet.setmTime(new Date());
//        proReportContactSheet.setmMreport(fileName);
//        proReportContactSheet.setmMcontactSheet(fileName1);
        proReportContactSheet.setmMforPayment(fileName2);
        proReportContactSheet.setmCheckProblem(fileName3);
        return toAjax(proReportContactSheetService.insertProReportContactSheet(proReportContactSheet));
    }

    /**
     * 修改维保报告书联系单
     */
    @GetMapping("/edit/{mId}")
    public String edit(@PathVariable("mId") Long mId, ModelMap mmap)
    {
        ProReportContactSheet proReportContactSheet = proReportContactSheetService.selectProReportContactSheetById(mId);
        mmap.put("proReportContactSheet", proReportContactSheet);
        return prefix + "/edit";
    }

    /**
     * 修改保存维保报告书联系单
     */
    @RequiresPermissions("ruoyi:sheet1:edit")
    @Log(title = "维保报告书联系单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProReportContactSheet proReportContactSheet)
    {
        return toAjax(proReportContactSheetService.updateProReportContactSheet(proReportContactSheet));
    }

    /**
     * 删除维保报告书联系单
     */
    @RequiresPermissions("ruoyi:sheet1:remove")
    @Log(title = "维保报告书联系单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(proReportContactSheetService.deleteProReportContactSheetByIds(ids));
    }
}
